package br.edu.fanor.gema.ejb.dao.morphia;

import java.net.UnknownHostException;

import com.github.jmkgreen.morphia.Datastore;
import com.github.jmkgreen.morphia.Morphia;
import com.mongodb.Mongo;
import com.mongodb.MongoClient;

public class MorphiaConn {
	
	private static final String DB_HOST="127.0.0.1";
	private static final String DB_NAME="gemasis";
	private Mongo mongo;
	private Datastore ds;
	
	//A documentação aconselha o padrão Singleton para conexão ao banco
	public Mongo getMongo(){
		if (mongo == null){
			
			try{
				mongo = new MongoClient(DB_HOST);
			}
			catch (UnknownHostException e){
				e.printStackTrace();
			}
		}
		
		return mongo;
	}
			
	public Datastore getDatastore(){
		if (ds == null){
			ds = new Morphia().createDatastore(getMongo(), DB_NAME);
		}
		return ds;
	}
	
}
